package com.yzy.soft.system.freemarker;

//import com.evada.inno.pm.code.generate.model.ColumnClass;

import freemarker.template.Template;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * ����������������
 * Created by Ay on 2017/5/1.
 */
public class TestCodeGenerateUtils {


    private FileInfo fileInfo;
    private final String AUTHOR = "Yinzhiyuan";
//        private final String CURRENT_DATE = "2017/05/03";
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    Calendar calendar = Calendar.getInstance();
    private final String CURRENT_DATE = format.format(calendar.getTime());
    private final String tableName = "tm_project_quality_problem";

//    public FileInfo getfileInfo() {
//
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
//        Calendar calendar = Calendar.getInstance();
//        fileInfo = new FileInfo();
//        fileInfo.setAuthor("Yinzhiyuan");
//        fileInfo.setCreateTime(format.format(calendar.getTime()));
//        fileInfo.setEncoding("UTF-8");
//        return fileInfo;
//    }

    private final static String diskPath = "D:\\BaiduYunDownload\\liscw-parent\\IFI\\src\\main\\java";
//    private final String changeTableName = replaceUnderLineAndUpperCase(tableName);

//    public Connection getConnection() throws Exception{
//        Class.forName(DRIVER);
//        Connection connection= DriverManager.getConnection(URL, USER, PASSWORD);
//        return connection;
//    }

    //基础信息

//    private final static String[] upperTableName=new String[]{"Baseinfo", "Faq", "Navbarinfo", "Price", "Priceversion", "Priceversionfeature", "Productionfeature", "Productioninfo", "Productionshow"};
//    private final static String[] lowerTableName=new String[]{"baseinfo", "faq", "navbarinfo", "price", "priceversion", "priceversionfeature", "productionfeature", "productioninfo", "productionshow"};

    private String[] upperTableName = new String[]{"Production"};
    private String[] lowerTableName = new String[]{"production"};
    //项目相关
    private final static String project_name = "ifi";

    //controller相关
    private final static String path_controller = "//com//yzy//soft//system//controller//";

    private final static String packageName = "com.sinosoft.ifi.system";
    private final static String[] tableAnnotation = new String[]{"业务交易模板"};
    private String[] controllerFileName = upperTableName;
    private final static String[] class_remark = new String[]{};
    private String[] class_requestMapping = lowerTableName;


    //mapper接口相关
    private String[] mapperName = upperTableName;
    private final String path_mapper = "//com//yzy//soft//common//persistence//dao//";
    private String[] mapper_tablename = upperTableName;
    private final String mapper_packageName = "com.yzy.soft.common.persistence.dao";

    //mapperXML文件相关
    private String[] mapperXMLName = upperTableName;
    private final String path_mapperXML = "//com//yzy//soft//common//persistence//dao//xml//";
    private String[] mapperXML_tablename = upperTableName;
    private final String mapperXML_packageName = "com.yzy.soft.common.persistence.model";

    //Dto相关
    private final String path_dto = "//com//yzy//soft//system//dto//";
    private String[] dto_tablename = upperTableName;
    private final String dto_packageName = "com.yzy.soft.system.dto";

    public TestCodeGenerateUtils() {

    }

    public TestCodeGenerateUtils(String[] upperTableName, String[] lowerTableName) {
        this.upperTableName = upperTableName;
        this.lowerTableName = lowerTableName;
        this.controllerFileName = upperTableName;
        this.class_requestMapping = lowerTableName;
        this.mapperName = upperTableName;
        this.mapper_tablename = upperTableName;
        this.mapperXMLName = upperTableName;
        this.mapperXML_tablename = upperTableName;
        this.dto_tablename = upperTableName;
    }


    public static void main(String[] args) throws Exception {
        TestCodeGenerateUtils testCodeGenerateUtils = new TestCodeGenerateUtils();
        testCodeGenerateUtils.generate();
    }

    public void generate() throws Exception {
        try {
//            Connection connection = getConnection();
//            DatabaseMetaData databaseMetaData = connection.getMetaData();
//            ResultSet resultSet = databaseMetaData.getColumns(null,"%", tableName,"%");
            //生成Mapper接口文件
            generateMapperFile();
            //生成MapperXML配置文件
            generateMapperXMLFile();
//            //����Dao�ļ�
//            generateDaoFile(resultSet);
//            //����Repository�ļ�
//            generateRepositoryFile(resultSet);
//            //���ɷ����ӿ��ļ�
//            generateServiceInterfaceFile(resultSet);
//            //���ɷ���ʵ�ֲ��ļ�
//            generateServiceImplFile(resultSet);
            //生成Controller控制层文件
            generateControllerFile();
//            //生成DTO数据传输对象文件
            generateDTOFile();
//            //����Model�ļ�
//            generateModelFile(resultSet);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {

        }
    }


    private void generateControllerFile() throws Exception {
        final String suffix = "Controller.java";
        final String templateName = "Controller.ftl";
        File file = new File(diskPath + path_controller);
        if (!file.exists()) {
            file.createNewFile();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("dto_packageName", dto_packageName);
        for (int i = 0; i < dto_tablename.length; i++) {
//            dataMap.put("dto_tablename",dto_tablename[i]);

            dataMap.put("table_annotation", tableAnnotation[0]);
            dataMap.put("class_requestMapping", class_requestMapping[i]);
            dataMap.put("table_name", controllerFileName[i]);
            String path = diskPath + path_controller + controllerFileName[i] + suffix;
            File controllerFile = new File(path);
            generateFileByTemplate(templateName, controllerFile, dataMap);
        }
    }


    //    private void generateModelFile(ResultSet resultSet) throws Exception{
//
//        final String suffix = ".java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "Model.ftl";
//        File mapperFile = new File(path);
//        List<ColumnClass> columnClassList = new ArrayList<ColumnClass>();
//        ColumnClass columnClass = null;
//        while(resultSet.next()){
//            //id�ֶ��Թ�
//            if(resultSet.getString("COLUMN_NAME").equals("id")) continue;
//            columnClass = new ColumnClass();
//            //��ȡ�ֶ�����
//            columnClass.setColumnName(resultSet.getString("COLUMN_NAME"));
//            //��ȡ�ֶ�����
//            columnClass.setColumnType(resultSet.getString("TYPE_NAME"));
//            //ת���ֶ����ƣ��� sys_name ��� SysName
//            columnClass.setChangeColumnName(replaceUnderLineAndUpperCase(resultSet.getString("COLUMN_NAME")));
//            //�ֶ������ݿ��ע��
//            columnClass.setColumnComment(resultSet.getString("REMARKS"));
//            columnClassList.add(columnClass);
//        }
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        dataMap.put("model_column",columnClassList);
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//
//    }
//
    private void generateDTOFile() throws Exception {
        final String suffix = "Dto.java";
        final String templateName = "DTO.ftl";
//        final String templateName = "Mapper.ftl";
        File file = new File(diskPath + path_dto);
        if (!file.exists()) {
            file.createNewFile();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        dataMap.put("dto_packageName", dto_packageName);
        for (int i = 0; i < dto_tablename.length; i++) {
            dataMap.put("dto_tablename", dto_tablename[i]);

            String path = diskPath + path_dto + dto_tablename[i] + suffix;
            File dtoFile = new File(path);
            generateFileByTemplate(templateName, dtoFile, dataMap);
        }
    }


    //
//    
//
//    private void generateServiceImplFile(ResultSet resultSet) throws Exception{
//        final String suffix = "ServiceImpl.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "ServiceImpl.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateServiceInterfaceFile(ResultSet resultSet) throws Exception{
//        final String prefix = "I";
//        final String suffix = "Service.java";
//        final String path = diskPath + prefix + changeTableName + suffix;
//        final String templateName = "ServiceInterface.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateRepositoryFile(ResultSet resultSet) throws Exception{
//        final String suffix = "Repository.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "Repository.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//    }
//
//    private void generateDaoFile(ResultSet resultSet) throws Exception{
//        final String suffix = "DAO.java";
//        final String path = diskPath + changeTableName + suffix;
//        final String templateName = "DAO.ftl";
//        File mapperFile = new File(path);
//        Map<String,Object> dataMap = new HashMap<String,Object>();
//        generateFileByTemplate(templateName,mapperFile,dataMap);
//
//    }
//
    private void generateMapperFile() throws Exception {
        final String suffix = "Mapper.java";
//        final String path = diskPath + path_mapper+mapperName + suffix;
        final String templateName = "Mapper.ftl";
        File file = new File(fileInfo.getDiskPath() + path_mapper);
        if (!file.exists()) {
            file.createNewFile();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        for (int i = 0; i < mapperName.length; i++) {
            dataMap.put("mapper_tablename", mapper_tablename[i]);
            dataMap.put("mapper_packageName", mapper_packageName);

            String path = diskPath + path_mapper + mapperName[i] + suffix;
            File mapperFile = new File(path);

            generateFileByTemplate(templateName, mapperFile, dataMap);
        }


    }

    private void generateMapperXMLFile() throws Exception {
        final String suffix = "Mapper.xml";
//        final String path = diskPath + path_mapper+mapperName + suffix;
        final String templateName = "MapperXML.ftl";
        File file = new File(diskPath + path_mapperXML);
        if (!file.exists()) {
            file.createNewFile();
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        for (int i = 0; i < mapperXMLName.length; i++) {
            dataMap.put("mapperXML_tablename", mapperXML_tablename[i]);
            dataMap.put("mapperXML_packageName", mapperXML_packageName);

            String path = diskPath + path_mapperXML + mapperXMLName[i] + suffix;
            File mapperFile = new File(path);

            generateFileByTemplate(templateName, mapperFile, dataMap);
        }


    }

    //
    private void generateFileByTemplate(final String templateName, File file, Map<String, Object> dataMap) throws Exception {
        if (file.exists()) {
            file.deleteOnExit();
        }
        Template template = FreeMarkerTemplateUtils.getTemplate(templateName);
        FileOutputStream fos = new FileOutputStream(file);
        dataMap.put("table_name_small", tableName);

        dataMap.put("author", AUTHOR);
        dataMap.put("date", CURRENT_DATE);
        dataMap.put("package_name", packageName);

        dataMap.put("project_name", project_name);


        Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"), 10240);
        template.process(dataMap, out);
    }
//

}
 

